package org.dromara.common.mybatis.v1;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.dromara.common.mybatis.v1.support.SqlKeyword;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class ServiceImpl<M extends BaseMapper<T>, T> extends com.baomidou.mybatisplus.extension.service.impl.ServiceImpl<M, T> {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    @Transactional(
        rollbackFor = {Exception.class}
    )
    public boolean save(T entity) {
        EntityUtils.setCreatAndUpdatInfo(entity);
        return super.save(entity);
    }

    @Override
    @Transactional(
        rollbackFor = {Exception.class}
    )
    public boolean saveBatch(Collection<T> entityList, int batchSize) {
        for (T entity : entityList) {
            EntityUtils.setCreatAndUpdatInfo(entity);
        }
        return super.saveBatch(entityList, batchSize);
    }

    @Override
    @Transactional(
        rollbackFor = {Exception.class}
    )
    public boolean saveOrUpdate(T entity) {
        EntityUtils.setCreatAndUpdatInfo(entity);
        return super.saveOrUpdate(entity);
    }

    @Override
    @Transactional(
        rollbackFor = {Exception.class}
    )
    public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
        for (T entity : entityList) {
            EntityUtils.setCreatAndUpdatInfo(entity);
        }
        return super.saveOrUpdateBatch(entityList, batchSize);
    }

    @Override
    @Transactional(
        rollbackFor = {Exception.class}
    )
    public boolean updateById(T entity) {
        EntityUtils.setUpdatedInfo(entity);

        return super.updateById(entity);
    }

    @Override
    @Transactional(
        rollbackFor = {Exception.class}
    )
    public boolean update(T entity, Wrapper<T> updateWrapper) {
        if (entity != null)
            EntityUtils.setUpdatedInfo(entity);
        return super.update(entity, updateWrapper);
    }

    @Override
    @Transactional(
        rollbackFor = {Exception.class}
    )
    public boolean updateBatchById(Collection<T> entityList, int batchSize) {
        for (T entity : entityList) {
            EntityUtils.setUpdatedInfo(entity);
        }

        return super.updateBatchById(entityList, batchSize);
    }


    @Override
    public T getOne(Wrapper<T> queryWrapper) {

        return super.getOne(queryWrapper, false);
    }


    public T getOne(Map<String, Object> params) {
        return getOne(SqlKeyword.buildCondition(params, new QueryWrapper<>()));
    }

    public List<T> list(Map<String, Object> params) {
        return list(SqlKeyword.buildCondition(params, new QueryWrapper<>()));
    }

    public List<Map<String, Object>> listMaps(Map<String, Object> params) {
        return listMaps(SqlKeyword.buildCondition(params, new QueryWrapper<>()));
    }
}
